<div class="bjui-pageHeader">
        <div class="bjui-searchBar">
            <span><a class="btn btn-orange" href="javascript:;" onclick="$(this).navtab('reloadForm', true);" data-icon="undo">刷新</a></span>
            <span><a id="export" class="btn btn-green" href="javascript:;" autocomplete="off">立即备份</a></span>
        </div>
</div>
<div class="bjui-pageContent tableContent">
    <form id="export-form" method="post" action="{:U('export')}">
        <table data-toggle="tablefixed" data-width="100%" data-layout-h="0" data-nowrap="true">
            <thead>
            <tr>
                <th width="48" height="30"><input class="check-all" checked="chedked" type="checkbox" value=""></th>
                <th>表名</th>
                <th width="120">数据量</th>
                <th width="120">数据大小</th>
                <th width="160">创建时间</th>
                <th width="160">备份状态</th>
                <th width="120">操作</th>
            </tr>
            </thead>
            <tbody>
            <volist name="list" id="table">
                <tr>
                    <td class="num">
                        <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                    </td>
                    <td>{$table.name}</td>
                    <td>{$table.rows}</td>
                    <td>{$table.data_length|format_bytes}</td>
                    <td>{$table.create_time}</td>
                    <td class="info">未备份</td>
                    <td class="action">
                        <a href="{:U('optimize?tables='.$table['name'].'&navTabId=database')}" data-toggle="doajax" data-confirm-msg="确定要操作吗？">优化表</a>&nbsp;
                        <a href="{:U('repair?tables='.$table['name'].'&navTabId=database')}" data-toggle="doajax" data-confirm-msg="确定要操作吗？"> 修复表</a>
                    </td>
                </tr>
            </volist>
            </tbody>
        </table>
    </form>
</div>
<!-- /应用列表 -->


<script type="text/javascript">
    (function ($) {
        var $form = $("#export-form"), $export = $("#export"), tables

        /**
         * 点击备份按钮
         */
        $export.click(function () {
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                    $form.attr("action"),
                    $form.serialize(),
                    function (data) {
                        if (data.statusCode == 200) {
                            tables = data.tables;
                            $export.html(data.message + "开始备份，请不要关闭本页面！");
                            backup(data.tab);
                            window.onbeforeunload = function () {
                                return "正在备份数据库，请不要关闭！"
                            }
                        } else {
                            $export.parent().children().removeClass("disabled");
                            $export.html("立即备份");
                        }
                    },
                    "json"
            );
            return false;
        });


        /**
         * 备份操作
         * @param tab
         * @param status
         */
        function backup(tab, status) {
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function (data) {
                if (data.statusCode == 200) {
                    showmsg(tab.id, data.message);

                    if (!$.isPlainObject(data.tab)) {
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function () {
                            return null
                        }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                }
            }, "json");

        }

        /**
         * 修改
         * @param id
         * @param msg
         */
        function showmsg(id, msg) {
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);


</script>